﻿using System;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
using Elasticsearch.Synchronize.Job.AutoMapper;
using Elasticsearch.Synchronize.Job.Job;
using Elasticsearch.Synchronize.Job.Tailer;
using Hangfire;
using Hangfire.Redis;

namespace Elasticsearch.Synchronize.Job
{
    class Program
    {
        static void Main(string[] args)
        {
            Mapper.Initialize(cfg => cfg.AddProfile(new SimpleMapProfile()));

            // The Microsoft.Extensions.DependencyInjection.ServiceCollection
            // has extension methods provided by other .NET Core libraries to
            // regsiter services with DI.
            var serviceCollection = new ServiceCollection();

            // The Microsoft.Extensions.Logging package provides this one-liner
            // to add logging services.
            serviceCollection.AddLogging();

            var builder = new ContainerBuilder();
            builder.RegisterModule(new ServiceDependencyModule());

            // Once you've registered everything in the ServiceCollection, call
            // Populate to bring those registrations into Autofac. This is
            // just like a foreach over the list of things in the collection
            // to add them to Autofac.
            builder.Populate(serviceCollection);

            // Make your Autofac registrations. Order is important!
            // If you make them BEFORE you call Populate, then the
            // registrations in the ServiceCollection will override Autofac
            // registrations; if you make them AFTER Populate, the Autofac
            // registrations will override. You can make registrations
            // before or after Populate, however you choose.
            //containerBuilder.RegisterType<MessageHandler>().As<IHandler>();

            // Creating a new AutofacServiceProvider makes the container
            // available to your app using the Microsoft IServiceProvider
            // interface so you can use those abstractions rather than
            // binding directly to Autofac.
            var container = builder.Build();

            var provider = new AutofacServiceProvider(container);
            using (var scope = provider.CreateScope())
            {
                provider.GetService<EventJoinTailer>().Run();
            }

            var option = provider.GetService<JobServerOption>();
            GlobalConfiguration.Configuration.UseAutofacActivator(container);
            GlobalConfiguration.Configuration.UseRedisStorage(option.RedisServer, new RedisStorageOptions()
            {
                Db = option.RedisDbIndex,
                UseTransactions = true,
                Prefix = option.RedisKeyPrefix
            });

            JobBuilder.Build();

            using (var server = new BackgroundJobServer())
            {
                Console.WriteLine("Hangfire Server started. Press any key to exit...");
                Console.ReadKey();
            }
        }
    }
}